package com.zusmart.core.config.support;

import com.zusmart.base.util.StringUtils;

public class DefaultConfigName extends AbstractConfigName {

	public DefaultConfigName(String originConfigName) {
		super(originConfigName);
	}

	@Override
	protected void resolve() {

		String configName = this.originConfigName;
		int index = -1;
		String prefix = "";

		int startIndex = configName.indexOf("(");
		int endIndex = configName.indexOf(")");
		if (startIndex + 1 == endIndex) {
			configName = configName.substring(0, startIndex);
		} else {
			if (startIndex != -1 && endIndex != -1 && startIndex < endIndex) {
				prefix = configName.substring(startIndex + 1);
				prefix = prefix.substring(0, prefix.indexOf(")"));
				configName = configName.substring(0, startIndex);
				if (!prefix.endsWith(".")) {
					prefix += ".";
				}
				this.actualPrefixValue = prefix;
			} else {
				this.actualPrefixValue = "";
			}
		}
		if ((index = configName.indexOf(":")) != -1) {
			this.actualConfigName = configName.substring(0, index);
			this.actualConfigValue = configName.substring(index + 1);
		} else {
			this.actualConfigName = configName;
		}
		if ((index = this.actualConfigName.indexOf("[")) != -1) {
			this.actualConfigFile = this.actualConfigName.substring(0, index);
			this.actualConfigName = this.actualConfigName.substring(index + 1);
			if (null != this.actualConfigValue && this.actualConfigValue.endsWith("]")) {
				this.actualConfigValue = this.actualConfigValue.substring(0, this.actualConfigValue.length() - 1);
			}
			if (this.actualConfigName.endsWith("]")) {
				this.actualConfigName = this.actualConfigName.substring(0, this.actualConfigName.length() - 1);
			}
		}
		if (StringUtils.isBlank(this.actualConfigName)) {
			throw new IllegalArgumentException(String.format("config name format error %s", this.actualConfigName));
		}
	}

}